using System.Collections.Generic;
using System.Linq;
using RFS.BizLogic.PaiShi.Ports.Adapters;
using RFS.Domain.PaiShi.Accounting;

namespace RFS.BizLogic.PaiShi.Ports
{
    
    
    public class AvsRepoMysqlAdapter : IAVSRepoPort
    {
        private IRepoPaiShi _rProd;
        private IRepoAcc _rAcc;

        public AvsRepoMysqlAdapter(IRepoPaiShi r, IRepoAcc rAcc)
        {
            _rProd = r;
            _rAcc = rAcc;
        }

        public void Save(IDayBasedWorkFlowNodeAccountingVoucherSetWrapper o)
        {
            
            //avs to  entity
            //1 save the obj graph to db as an complete
            //2 save it to dic for searching. safe
            //3 save its orderIds for orderFiltering service. it is safe too.

            // var dic = o.ToAccItemAndOrderBasedDicItem();
            var en = o.ToEntity();
            _rAcc.SaveEntity( en  );
            _rProd.SaveRecordedOrderIds(en);
            _rAcc.SaveTheAvsListItem(o);

        }

        public void Delete(ulong id)
        {
                
            //avs to  entity
            //1 del the obj graph to db as an complete
            //2 del it to dic for searching. safe
            //3 del its orderIds for orderFiltering service. it is safe too.

            var avs= _rAcc.GetById( id );
            _rAcc.DeleteEntity( avs ); // 1 2

    
            
            _rProd.DeleteRecordedOrderIds(avs.Id);

        }

        public IDayBasedWorkFlowNodeAccountingVoucherSetWrapper GetById(ulong id)
        {
            
            return _rAcc.GetById( id ).ToDo();
        }

        public IEnumerable<AVSListItemEntity> GetTheLastesRecords(int days = 7 )
        {
           var res= _rAcc.GetTheLastestRecords(days) ;

           return res;
        }
    }

    public class AvsRepoEntity
    {
        
    }
}